#!/bin/sh
out=./obj
check_output() {
    cmp $out/output.$1 $out/expected_output.$1 2>/dev/null >/dev/null
    if [ $? -eq 0 ]
    then
        echo "PASSED"
        #rm $1 $2
    else
        echo "FAILED"
        echo "Output from project2 binary is in file: .output.$1"
        echo "Expected output is in file: .expected_output.$1"
        echo "Debug output from project2 binary is in file: .debug_output.$1"
    fi
    echo
}

echo "###########################################################################"
echo "NOTE: Guidelines for using this script:"
echo "      - Tests check output written to stdout only."
echo "        If you have debug output in stdout, it may cause test failures."
echo "      - This is not a comprehensive set of test cases."
echo "        It is intended to help you do basic checks on your project."
echo "      - If your program fails any test, it will print the output files"
echo "        generated by your program and the output expected for the test case."
echo "      - The script should take about 20 seconds to complete."
echo "###########################################################################"
echo
echo
echo "Rebuilding project2..."
make -j4 project2
./obj/.client
echo "Done rebuilding project2"
echo


## Test Case 1
echo -n "Test case 1: Sending 1 packet and expecting receiver to print it out: "
(sleep 0.5; echo "msg 0 0 Hello world"; sleep 0.5; echo "exit") | ./project2 -r 1 -s 1 > $out/output.1 2> $out/debug_output.1

cat > $out/expected_output.1 << EOF
<RECV-0>:[Hello world]
EOF

check_output 1


## Test Case 2
echo -n "Test case 2: Sending 10 packets and expecting receiver to print them out in order: "
(sleep 0.5; for i in `seq 1 10`; do echo "msg 0 0 Packet: $i"; sleep 0.1; done; sleep 1; echo "exit") | ./project2 -r 1 -s 1 > $out/output.2 2> $out/debug_output.2

(for i in `seq 1 10`; do echo "<RECV-0>:[Packet: $i]"; done) > $out/expected_output.2

check_output 2


## Test Case 3
echo -n "Test case 3: Sending long message and expecting sender to partition it out: "
(sleep 0.5; echo "msg 0 0 12345678#1@2345678#2@2345678#3@2345678#4@2345678#5@2345678#6@2345678#7@2345678#8@2345678#9"; sleep 0.5; echo "exit") |
 ./project2 -r 1 -s 1 > $out/output.3 2> $out/debug_output.3

echo
cat $out/output.3
echo

## Test Case 4
echo -n "Test case 4: Sending 10 packets (with corrupt probability of 50%) and expecting receiver to print them out in order: "
(sleep 0.5; for i in `seq 1 50`; do echo "msg 0 0 Packet: $i"; sleep 0.01; done; sleep 5; echo "exit") | ./project2 -c 0.5 -r 1 -s 1 > $out/output.4 2> $out/debug_output.4

(for i in `seq 1 50`; do echo "<RECV-0>:[Packet: $i]"; done) > $out/expected_output.4

check_output 4


## Test Case 5
echo -n "Test case 5: Sending 10 packets (with corrupt probability of 20% and drop probability of 20%) and expecting receiver to print them out in order: "
(sleep 0.5; for i in `seq 1 30`; do echo "msg 0 0 Packet: $i"; sleep 0.1; done; sleep 5; echo "exit") | ./project2 -d 0.5 -c 0.5 -r 1 -s 1 > $out/output.5 2> $out/debug_output.5

(for i in `seq 1 30`; do echo "<RECV-0>:[Packet: $i]"; done) > $out/expected_output.5

check_output 5

## Test Case 6
echo -n "Test case 6: Sending 10 packets (with corrupt probability of 35% and drop probability of 35%) and expecting receiver to print them out in order: "
(sleep 0.5; for i in `seq 1 30`; do echo "msg 0 0 Packet: $i"; sleep 0.1; done; sleep 5; echo "exit") | ./project2 -d 0.35 -c 0.35 -r 1 -s 1 > $out/output.6 2> $out/debug_output.6

(for i in `seq 1 30`; do echo "<RECV-0>:[Packet: $i]"; done) > $out/expected_output.6

check_output 6

echo
echo "Completed test cases"

